/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package guiPlaylisted.view;

import guiPlaylisted.PlaylistedApp;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFrame;

import controlPlaylisted.UserController;

import dataPlaylisted.User;

/**
 *
 * @author Administrador
 */
public class UserView {

    private User m_user;
    
    private UserController m_userController;

    private JDialog m_userSignIn;
    private JDialog m_userLogin;
    private JDialog m_DlgUserView;

    private static UserView instance;

    public UserView() throws FileNotFoundException, IOException, ClassNotFoundException{
         m_userController = UserController.getInstance();
         m_user = m_userController.getUser();
    }

    public static UserView getInstance() throws FileNotFoundException, IOException, ClassNotFoundException{
        if(instance == null)
            instance = new UserView();
        return instance;
    }

    public void runSignIn(){
         //if (m_userSignIn == null) {
            JFrame mainFrame = PlaylistedApp.getApplication().getMainFrame();
            m_userSignIn = new guiCadastroUsuario(mainFrame,true);
            m_userSignIn.setLocationRelativeTo(mainFrame);
        //}
        PlaylistedApp.getApplication().show(m_userSignIn);
    }

    public void runLogin(){
        JFrame mainFrame = PlaylistedApp.getApplication().getMainFrame();
        m_userLogin = new guiLogin(mainFrame, true);
        m_userLogin.setLocationRelativeTo(mainFrame);
        PlaylistedApp.getApplication().show(m_userLogin);
    }

    public void runUserView() throws IOException, FileNotFoundException, ClassNotFoundException{
        JFrame mainFrame = PlaylistedApp.getApplication().getMainFrame();
        m_DlgUserView = new guiUserView(mainFrame, true);
        m_DlgUserView.setLocationRelativeTo(mainFrame);
        PlaylistedApp.getApplication().show(m_DlgUserView);
    }

    public void setUser(){
       m_user = m_userController.getUser();
    }

    public User getUser(){
       return m_user;
    }

    public void saveData(String _login, String _pass, String _userName) throws IOException, ClassNotFoundException{
        try{
            m_userController.setUserOnList(_login, _pass, _userName);
        }
        catch(IOException e){
        }
    }

    public void makeLogin(String _login, String _pass) throws ClassNotFoundException, FileNotFoundException, IOException{
        m_userController.login(_login, _pass);
        if(m_userController.getLogged()){
            System.out.println(m_user.getUserLogin());
            this.runUserView();
        }
    }


  

}
